var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";

var upWindow = window.parent.frames["zwIframe"];
if (!upWindow) {
    try {
        upWindow = window.parent.frames["componentDiv"].frames["zwIframe"];
    } catch (e) {
    }
    if (!upWindow) {
        upWindow = window.parent.frames["main"].frames["zwIframe"];
    }
}
//关闭自定义扩张控件弹出窗口
function fy_dialog_close() {
    window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close")
            .trigger("click");
}
function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}

function getUpFieldValue(field) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    
    var obj = new Object();
    obj.displayName = fieldVal.displayName;
    obj.name = fieldVal.name;
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        default:
            tag = false;
    }
    if (obj.value != undefined) {
        obj.value = obj.value.replace("\r\n", ";");
        obj.value = obj.value.replace("\n", ";");
    }
    return obj;
}
//设置值
function setUpFieldValue2(field, value, displayValue, index) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":
            if (editTag) {
                field.find("#" + fieldVal.name).val(value);
                field.find("#" + fieldVal.name).trigger("blur");
            } else if (addTag) {
                field.find("#" + fieldVal.name).val(
                        field.find("#" + fieldVal.name).val() + value);
                field.find("#" + fieldVal.name).trigger("blur");
            }
            break;
        case "date":
            if (editTag) {
                field.find("#" + fieldVal.name).val(value);
                field.find("#" + fieldVal.name).trigger("blur");
            }
            break;
    }
}

var jobNums = [];
var xmlObj = null; // 定义XMLHttpRequest对象
var jobNumInputArray = getUpFieldsByFieldval("工号");
var nameInputArray = getUpFieldsByFieldval("姓名");
var jobInputArray = getUpFieldsByFieldval("岗位");


function init() {
    for (var i = 0, length =  jobNumInputArray.length; i < length; i++) {
    	 jobNums.push(getUpFieldValue(jobNumInputArray.eq(i)).value);
        setUpFieldValue2(nameInputArray.eq(i), "");
        setUpFieldValue2(jobInputArray.eq(i), ""); 
    }
    
}


function load() {
if (window.XMLHttpRequest) {// 如果当前浏览器支持XMLHttpRequest则创建XMLHttpRequest对象
        xmlObj = new XMLHttpRequest();
    } else {
        if (window.ActiveXObject) {// 如果当前浏览器支持ActiveXObject则创建ActiveXObject对象
            xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            return;
        }
    }
    xmlObj.onreadystatechange = handleResponse;// 判断URL调用的状态值并处理
xmlObj.open("get",
            "./oa2hrController.do?method=jobNumFindPersonInfo&jobNums="
            +jobNums, true);
    xmlObj.send();

}
//用于处理状态的函数
function handleResponse() {
	   if (xmlObj.readyState == 4){
		   var success =true;
        if (xmlObj.status == 200) {
            var datas = eval("[" + xmlObj.responseText + "]");
            if (datas.length == 1) {
                var personMap = datas[0];
            for (var i = 0, length =  jobNumInputArray.length; i < length; i++) {
            	   var person = personMap["jobNum"+ getUpFieldValue(jobNumInputArray.eq(i)).value];
            	   if (person) {
                   setUpFieldValue2(nameInputArray.eq(i),
                           person["name"]);
                   setUpFieldValue2(jobInputArray.eq(i),
                           person["job"]);                             
            	   }
            	    success = true;
            	}
          
          } 
       }
        if (!success) {
            alert("请检查输入的工号");
      }
 	   fy_dialog_close();
	   }
	   

}
init();
load();
